package org.jeecg.modules.wlhy.utils;

import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.wlhy.hycfg.entity.HyCfg;
import org.jeecg.modules.wlhy.hycfg.entity.HyCfgItem;
import org.jeecg.modules.wlhy.hycfg.service.IHyCfgItemService;
import org.jeecg.modules.wlhy.hycfg.service.IHyCfgService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HyCfgUtil {
    //region base

    private static boolean isClustered;

    private static Map<String, HyCfg> _cfgList = new HashMap<>();
    private static Map<String,Map<String,String>> _cfgItemList = new HashMap<>();

    private static IHyCfgService _hyCfgService;
    private static IHyCfgItemService _hyCfgItemSerivce;

//    public HyCfgUtil(@Autowired HyCfgService hyCfgService, @Autowired CfgItemService hyCfgItemService){
//        _hyCfgService = hyCfgService;
//        _hyCfgItemSerivce = hyCfgItemService;
//    }

    @Value(value="${params.isClustered}")
    public void setClustered(boolean st) {
        isClustered = st;
    }

    public static void initService(IHyCfgService hyCfgService, IHyCfgItemService hyCfgItemService){
        _hyCfgService = hyCfgService;
        _hyCfgItemSerivce = hyCfgItemService;
    }

    private static Map<String,String> getChildMap( List<HyCfgItem> cfgItemList , String pid){
        Map<String,String> child = new HashMap<>();
        for (HyCfgItem hyCfgItem : cfgItemList) {
            if(pid.equals(hyCfgItem.getPid())){
                child.put(hyCfgItem.getItemText(),hyCfgItem.getItemValue());
            }
        }
        return child;
    }
    public static void init() {

        _cfgList.clear();
        _cfgItemList.clear();

        List<HyCfg> cfgList = _hyCfgService.list();
        List<HyCfgItem> cfgItemList = _hyCfgItemSerivce.list();

        for (HyCfg hyCfg : cfgList) {
            String cfgCode = hyCfg.getCfgCode();
            _cfgList.put(cfgCode, hyCfg);
            Map<String, String> child = getChildMap(cfgItemList, hyCfg.getId());
            _cfgItemList.put(cfgCode, child);
        }


//        HyCfg tonglian = getCfg2("tonglian");
//        if(tonglian != null) {
//            Map<String, String> tonglianCfgItem = getCfgItem("tonglian");
//            TongLianUtil.initParams("1".equals(tonglian.getIzUsed()), tonglianCfgItem);
//        }
    }

    public static HyCfg getCfg(String cfgCode) {
        if (isClustered) {
            return _hyCfgService.getByCode(cfgCode);
        }
        return _cfgList.get(cfgCode);
    }

    //配置不存在时，抛异常
    public static boolean isUsed(String cfgCode){
        HyCfg cfg = getCfg(cfgCode);
        if(cfg == null){
            throw new JeecgBootException("配置不存在,配置名称:" + cfgCode);
        }
        return "1".equals(cfg.getIzUsed());
    }
    //配置不存在，默认不开启
    public static boolean isUsed2(String cfgCode){
        HyCfg cfg = getCfg(cfgCode);
        if(cfg == null){
            return false;
        }
        return "1".equals(cfg.getIzUsed());
    }


    public static String getCfgValue(String cfgCode){
        HyCfg cfg = getCfg(cfgCode);
        if(cfg == null){
            throw new JeecgBootException("配置不存在,配置名称:" + cfgCode);
        }
        return cfg.getCfgValue();
    }

    public static String getCfgValue2(String cfgCode){
        HyCfg cfg = getCfg(cfgCode);
        if(cfg == null){
            return null;
        }
        return cfg.getCfgValue();
    }

    public static String getCfgItem(String cfgCode , String itemCode) {
        if (isClustered) {
            HyCfgItem item = _hyCfgItemSerivce.getByCode(cfgCode, itemCode);
            if (item == null) {
                throw new JeecgBootException("配置项不存在,配置项参数名称:" + itemCode);
            }
            return item.getItemValue();
        }else {
            if (!_cfgItemList.containsKey(cfgCode)) {
                throw new JeecgBootException("配置不存在,配置名称:" + cfgCode);
            }
            Map<String, String> childItems = _cfgItemList.get(cfgCode);
            if (!childItems.containsKey(itemCode)) {
                throw new JeecgBootException("配置项不存在,配置项参数名称:" + itemCode);
            }
            return childItems.get(itemCode);
        }
    }


    public static String getCfgItem2(String cfgCode , String itemCode) {
        if (isClustered) {
            HyCfgItem item = _hyCfgItemSerivce.getByCode(cfgCode, itemCode);
            if (item == null) {
                return null;
            }
            return item.getItemValue();
        }else {
            if (!_cfgItemList.containsKey(cfgCode)) {
                return null;
            }
            Map<String, String> childItems = _cfgItemList.get(cfgCode);
            if (!childItems.containsKey(itemCode)) {
                return null;
            }
            return childItems.get(itemCode);
        }
    }

//    public static Map<String, String> getCfgItem(String cfgCode) {
//        if(!_cfgItemList.containsKey(cfgCode)){
//            throw new JeecgBootException("配置不存在");
//        }
//        return  _cfgItemList.get(cfgCode);
//    }
    //endregion

    //region

    //通联支付
    public static boolean isTongLianUsed(){
        return isUsed2("tonglian");
    }

    //endregion
}
